package com.itheima.service;

import com.itheima.mapper.NewShoppingCartMapper;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.pojo.NewShoppingCart;
import com.itheima.pojo.ShoppingCart;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class NewShoppingCartService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    public List<NewShoppingCart> selectByBuyerId(Integer buyerId) {
        //调用NewShoppingCartMapper.selectByBuyerId()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取NewShoppingCartMapper
        NewShoppingCartMapper mapper = sqlSession.getMapper(NewShoppingCartMapper.class);

        //4. 调用方法
        List<NewShoppingCart> shoppingCartGoods = mapper.selectByBuyerId(buyerId);

        // 释放资源
        sqlSession.close();

        return shoppingCartGoods;
    }

    public List<NewShoppingCart> selectByBuyerIdAndItemId(Integer itemId, Integer buyerId) {
        //调用NewShoppingCartMapper.selectByBuyerIdAndItemId()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取NewShoppingCartMapper
        NewShoppingCartMapper mapper = sqlSession.getMapper(NewShoppingCartMapper.class);

        //4. 调用方法
        List<NewShoppingCart> shoppingCartGoods = mapper.selectByBuyerIdAndItemId(itemId, buyerId);

        // 释放资源
        sqlSession.close();

        return shoppingCartGoods;
    }

    public void updateItemQuantity(Integer itemId, Integer buyerId, Integer itemQuantity) {
        //调用NewShoppingCartMapper.updateItemQuantity()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取NewShoppingCartMapper
        NewShoppingCartMapper mapper = sqlSession.getMapper(NewShoppingCartMapper.class);

        //4. 调用方法
        mapper.updateItemQuantity(itemId, buyerId, itemQuantity);

        // 提交事务
        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }

    public void add(Integer itemId, Integer buyerId, Integer itemQuantity) {
        //调用NewShoppingCartMapper.add()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取NewShoppingCartMapper
        NewShoppingCartMapper mapper = sqlSession.getMapper(NewShoppingCartMapper.class);

        //4. 调用方法
        mapper.add(itemId, buyerId, itemQuantity);

        // 提交事务
        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }

    public void delete(Integer itemId, Integer buyerId) {
        //调用NewShoppingCartMapper.delete()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取NewShoppingCartMapper
        NewShoppingCartMapper mapper = sqlSession.getMapper(NewShoppingCartMapper.class);

        //4. 调用方法
        mapper.delete(itemId, buyerId);

        // 提交事务
        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }
}
